<template>
  <el-dialog title="生产商选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="150px">
      <el-row>
        <el-col :span="7">
          <el-form-item label="生产商编码" prop="manufactureCode">
            <el-input
              v-model="queryParams.manufactureCode"
              placeholder="请输入生产商编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="生产商名称" prop="manufactureName">
            <el-input
              v-model="queryParams.manufactureName"
              placeholder="请输入生产商名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label="生产商简称" prop="manufactureNick">
            <el-input
              v-model="queryParams.manufactureNick"
              placeholder="请输入生产商简称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>




    </el-form>
    <div style="height: 15px"></div>
    <el-table v-loading="loading" :data="manufactureList" @current-change="handleCurrent"  @row-dblclick="handleRowDbClick">
      <el-table-column width="55" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedManufactureId" :label="scope.row.manufactureId" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="外协生产商编码" align="center" prop="manufactureCode" >
          <template slot-scope="scope">
                <el-button
                  type="text"
                  @click="handleView(scope.row)"
                  v-hasPermi="['mes:md:outsourcedManufacture:query']"
                >{{scope.row.manufactureCode}}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="外协生产商名称" align="center" prop="manufactureName" width="150px" :show-overflow-tooltip="true"/>
      <el-table-column label="外协生产商简称" align="center" prop="manufactureNick" />
      <el-table-column label="外协生产商等级" align="center" prop="manufactureLevel" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_vendor_level" :value="scope.row.manufactureLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="外协生产商评分" align="center" prop="manufactureScore">
        <template slot-scope="scope">
          <el-rate :value="scope.row.manufactureScore" disabled></el-rate>
        </template>
      </el-table-column>
      <el-table-column label="外协生产商电话" align="center" prop="tel" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listOutsourcedManufacture, getOutsourcedManufacture, delOutsourcedManufacture, addOutsourcedManufacture, updateOutsourcedManufacture } from "@/api/mes/md/outsourcedManufacture";
import { getToken } from "@/utils/auth";
import {genCode} from "@/api/system/autocode/rule"
export default {
  name: "manufacture",
  dicts: ['erp_payment_type'],
  props:{
    oldManufactureId:"",
    manufacture: {
      type: Object,
      default: function(){
        return {'enableFlag': 'Y'}
      }} //外部传入的工单过滤信息
  },
  data() {
    return {
      selectedManufactureId:this.oldManufactureId,
      optType: undefined,
      // 遮罩层
      loading: true,
      showFlag: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协生产商表格数据
      manufactureList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/mes/md/outsourcedManufacture/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        manufactureCode: null,
        manufactureName: null,
        manufactureNick: null,
        manufactureEn: null,
        manufactureDes: null,
        manufactureLogo: null,
        manufactureLevel: null,
        manufactureScore: null,
        address: null,
        website: null,
        email: null,
        tel: null,
        contact1: null,
        contact1Tel: null,
        contact1Email: null,
        contact2: null,
        contact2Tel: null,
        contact2Email: null,
        creditCode: null,
        enableFlag: null,
      },
      // 表单参数
      form: {},

    };
  },
  created() {
    this.getList();
  },
watch:{
  oldManufactureId(newVal,old){
    this.selectedManufactureId=newVal;
  }
},
  methods: {
    handleCurrent(row){
      if(row){
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if(row){
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.selectedManufactureId=row.manufactureId;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.selectedManufactureId == null || this.selectedManufactureId == 0){
        this.$notify({
          title:'提示',
          type:'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    },
    /** 查询外协生产商列表 */
    getList() {
      this.loading = true;
      listOutsourcedManufacture(this.queryParams).then(response => {
        this.manufactureList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        manufactureId: null,
        manufactureCode: null,
        manufactureName: null,
        manufactureNick: null,
        manufactureEn: null,
        manufactureDes: null,
        manufactureLogo: null,
        manufactureLevel: "B",
        manufactureScore: null,
        address: null,
        website: null,
        email: null,
        tel: null,
        contact1: null,
        contact1Tel: null,
        contact1Email: null,
        contact2: null,
        contact2Tel: null,
        contact2Email: null,
        creditCode: null,
        enableFlag: 'Y',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    // 查询明细按钮操作
    handleView(row){
      this.reset();
      const manufactureId = row.manufactureId || this.ids
      getOutsourcedManufacture(manufactureId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看外协生产商";
        this.optType = "view";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.manufactureId != null) {
            updateOutsourcedManufacture(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOutsourcedManufacture(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
